//
//  MoreTableHeaderView.m
//  GameBaby
//
//  Created by keyZhang on 14-1-8.
//  Copyright (c) 2014年 keyZhang. All rights reserved.
//

#import "MoreTableHeaderView.h"
#import "WSZLViewController.h"
#import "UIView+Additions.h"
#import "MBProgressHUD.h"
#import "DataService.h"
#import "UIImageView+WebCache.h"

@implementation MoreTableHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.wjIDLabel.text = [NSString stringWithFormat:@"玩家ID：%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
    self.wjNameLabel.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"username"]];
    self.wjWndLabel.text = [NSString stringWithFormat:@"剩余蜗牛蛋：%@个",[[NSUserDefaults standardUserDefaults] objectForKey:@"wnd"]];
    
//    设置头像
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"headImgData"];
    if (data) {
        UIImage *headImage = [UIImage imageWithData:data];
        UIImage *newImg = [self circleImage:headImage withParam:1];
        [self.headImgBtn setBackgroundImage:newImg forState:UIControlStateNormal];
    }


}

- (IBAction)headImgBtnClick:(UIButton *)sender {
    
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                         destructiveButtonTitle:@"拍照"
                                              otherButtonTitles:@"相册", nil];
    [sheet showInView:self.viewController.view];
    
}


- (IBAction)wszlBtnClick:(UIButton *)sender {
    WSZLViewController *wszlVC = [[WSZLViewController alloc] init];
    [self.viewController.navigationController pushViewController:wszlVC animated:YES];
}


#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //来源
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //拍照
    if (buttonIndex == 0) {
        //判断是否有摄像头
        BOOL hasCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
        
        if (!hasCamera) {
            [(BaseViewController *)self.viewController showHUD:@"此设备没有摄像头" withHiddenDelay:2 withView:self.viewController.view];
            return;
        }
        
        sourceType = UIImagePickerControllerSourceTypeCamera;
    }else if (buttonIndex == 1){
        //相册
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }else if (buttonIndex == 2){
        //取消
        return;
    }
    
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.sourceType = sourceType;
    imgPicker.delegate = self;
    [self.viewController presentModalViewController:imgPicker animated:YES];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    
//    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
//    imgView.image = img;
    
//    UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(25, 25) radius:25 startAngle:0 endAngle:2*M_PI clockwise:YES];
//    CAShapeLayer* shape = [CAShapeLayer layer];
//    shape.path = path.CGPath;
//    imgView.layer.mask = shape;
    
    
//    [self.headImgBtn setImage:img forState:UIControlStateNormal];
    
    UIImage *newImg = [self circleImage:img withParam:1];
    
    [self.headImgBtn setBackgroundImage:newImg forState:UIControlStateNormal];
    
//    测试头像上传
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/user/uploadAvatar"];
    NSData *data = UIImageJPEGRepresentation(newImg, 0.3);
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"headImgData"];
    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",data,@"userAvator", nil];

    __weak MoreTableHeaderView *this = self;
    [DataService requestWithURL:urlStr
                         params:mDic
                     httpMethod:@"POST"
                    finishBlock:^(id result) {
                        [this loadDataFinished:result];
                    }];

    //隐藏picker
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    

}
- (void)loadDataFinished:(NSDictionary *)result
{
//    NSLog(@"%@",result);
}

//使img变圆形
-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {
    UIGraphicsBeginImageContext(image.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
	CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
	CGContextAddEllipseInRect(context, rect);
	CGContextClip(context);
	
	[image drawInRect:rect];
    CGContextAddEllipseInRect(context, rect);
	CGContextStrokePath(context);
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}


@end
